/**
 * ------------------------------------------------------------------------------------------
 * Copyright (c): Company, 2012 This software is the
 * proprietary information of FHWS, Würzburg All Right Reserved.
 * ------------------------------------------------------------------------------------------
 *
 *
 * @project: Vorlesungplan
 * @copyright FHWS 2012
 * @link       http://mussnocheingeragenwerden.de
 * @package    tt.model
 *
 * SVN revision information:
 *
 * @version $Rev$:
 * @author $Author$:
 * @date $Date$:
 */

package tt.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "lecture")
@XmlRootElement
@NamedQueries(
{
   @NamedQuery(name = "Lecture.findAll", query = "SELECT l FROM Lecture l  order by l.lecname"),
   @NamedQuery(name = "Lecture.findByIdlecture", query = "SELECT l FROM Lecture l WHERE l.idlecture = :idlecture"),
   @NamedQuery(name = "Lecture.findByLecname", query = "SELECT l FROM Lecture l WHERE l.lecname = :lecname"),
   @NamedQuery(name = "Lecture.findByLecshortcut", query = "SELECT l FROM Lecture l WHERE l.lecshortcut = :lecshortcut"),
   @NamedQuery(name = "Lecture.findByInfolecture", query = "SELECT l FROM Lecture l WHERE l.infolecture = :infolecture"),
   @NamedQuery(name = "Lecture.findByCourseOfStudyID", query = "SELECT l FROM Lecture l JOIN l.semester s JOIN s.courseofstudy c WHERE c.idcourse = :idcourse order by l.lecname")
})
public class Lecture implements Serializable, Comparable<Lecture>
{

   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idlecture")
   private Integer idlecture;
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 45)
   @Column(name = "lecname")
   private String lecname;
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 10)
   @Column(name = "lecshortcut")
   private String lecshortcut;
   @Size(max = 45)
   @Column(name = "infolecture")
   private String infolecture;
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "lecture")
   private Collection<Lecturedate> lecturedateCollection;
   @JoinColumn(name = "semester", referencedColumnName = "idsemester")
   @ManyToOne
   private Semester semester;
   @JoinColumn(name = "exam", referencedColumnName = "idexam")
   @ManyToOne
   private Exam exam;

   public Lecture()
   {
   }

   public Lecture getCopy()
   {
      Lecture my = new Lecture();
      my.setExam(exam);
      my.setIdlecture(idlecture);
      my.setInfolecture(infolecture);
      my.setLecname(lecname);
      my.setLecshortcut(lecshortcut);
      //my.setLecturedateCollection(lecturedateCollection);
      my.setSemester(semester);
      return my;
   }//endmehtod

   public Lecture(Integer idlecture)
   {
      this.idlecture = idlecture;
   }

   public Lecture(Integer idlecture, String lecname, String lecshortcut)
   {
      this.idlecture = idlecture;
      this.lecname = lecname;
      this.lecshortcut = lecshortcut;
   }

   public Integer getIdlecture()
   {
      return idlecture;
   }

   public void setIdlecture(Integer idlecture)
   {
      this.idlecture = idlecture;
   }

   public String getLecname()
   {
      return lecname;
   }

   public void setLecname(String lecname)
   {
      this.lecname = lecname;
   }

   public String getLecshortcut()
   {
      return lecshortcut;
   }

   public void setLecshortcut(String lecshortcut)
   {
      this.lecshortcut = lecshortcut;
   }

   public String getInfolecture()
   {
      return infolecture;
   }

   public void setInfolecture(String infolecture)
   {
      this.infolecture = infolecture;
   }

   @XmlTransient
   public Collection<Lecturedate> getLecturedateCollection()
   {
      Collections.sort((List<Lecturedate>) lecturedateCollection);
      return lecturedateCollection;
   }

   public void setLecturedateCollection(Collection<Lecturedate> lecturedateCollection)
   {
      this.lecturedateCollection = lecturedateCollection;
   }

   public Semester getSemester()
   {
      return semester;
   }

   public void setSemester(Semester semester)
   {
      this.semester = semester;
   }

   public Exam getExam()
   {
      return exam;
   }

   public void setExam(Exam exam)
   {
      this.exam = exam;
   }

   @Override
   public int hashCode()
   {
      int hash = 0;
      hash += (idlecture != null ? idlecture.hashCode() : 0);
      return hash;
   }

   @Override
   public boolean equals(Object object)
   {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Lecture))
      {
         return false;
      }
      Lecture other = (Lecture) object;
      if ((this.idlecture == null && other.idlecture != null) || (this.idlecture != null && !this.idlecture.equals(other.idlecture)))
      {
         return false;
      }
      return true;
   }

   @Override
   public String toString()
   {
      return "tt.model.Lecture[ idlecture=" + idlecture + " ]";
   }

   public int compareTo(Lecture o)
   {
      return lecname.compareToIgnoreCase(o.getLecname());

   }//endmethod
   public static Comparator<Lecture> SortByExamDate = new Comparator<Lecture>()
   {

      @Override
      public int compare(Lecture lec1, Lecture lec2)
      {

         //wenn beide kein Examen haben, sind sie gleich
         if (lec1.getExam() == null && lec2.getExam() == null)
         {
            return 0;
         } //lec1 hat keinen Termin,kommt nach hinten
         else if (lec1.getExam() == null)
         {
            return 1;
         } //lec2 hat kein Examen, kommt nach hinten
         else if (lec2.getExam() == null)
         {
            return -1;
         } else
         {
            int returnwert;

            returnwert = (lec1.getExam().getExamdate()).compareTo(lec2.getExam().getExamdate());
            //wenn datum gleich, vergleiche noch die uhrzeit
            if (returnwert == 0)
            {
               return (lec1.getExam().getStartofexam()).compareTo(lec2.getExam().getStartofexam());
            } else
            {
               return returnwert;
            }
            //Fälle, dass beide Examen schon einen Termin haben return 0 muss dann natürlich weg

         }

      }//end method
   };//end static
}
